/**
 * 
 */
package org.totalbeginner.tutorial;

import java.util.ArrayList;

/**
 * @author Ibraheem.Abdullah
 *
 */
public class MyLibrary {

	String name;
	ArrayList<Book> Books;
	ArrayList<Person> people;

	public MyLibrary(String name) {
		this.name = name;
		Books = new ArrayList<Book>();
		people = new ArrayList<Person>();
	}
	
	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the books
	 */
	public ArrayList<Book> getBooks() {
		return Books;
	}

	/**
	 * @return the people
	 */
	public ArrayList<Person> getPeople() {
		return people;
	}

	public void addBook(Book b1) {
		this.getBooks().add(b1);
	}

	public void removeBook(Book b1) {
		this.getBooks().remove(b1);
	}

	public void addPerson(Person p1) {
		this.getPeople().add(p1);
	}

	public void removePerson(Person p1) {
		this.getPeople().remove(p1);
	}

	public boolean checkout(Book b1, Person p1) {
		if(b1.getPerson() == null){
			if(this.getNumberOfCheckedoutBooksForPerson(p1) < p1.getMaximumBooks()){
				b1.setPerson(p1);
				return true;				
			}
		}			
		return false;
	}

	public boolean checkin(Book b1) {
		if(b1.getPerson() != null){
			b1.setPerson(null);
			return true;
		}
		return false;
	}

	public int getNumberOfCheckedoutBooksForPerson(Person p1) {
		int nReturn = 0;
		for (Book b : this.getBooks()) {
			Person pFromLib = b.getPerson();
			if(pFromLib != null){
				if(pFromLib.getName().equals(p1.getName()))
					nReturn++;	
			}			
		}
		return nReturn;
	}

	public ArrayList<Book> getBooksForPerson(Person p1) {
		ArrayList<Book> bList = new ArrayList<Book>();
		for (Book b : this.getBooks()) {
			Person pFromLib = b.getPerson();
			if(pFromLib != null){
				if(pFromLib.getName().equals(p1.getName())){
					bList.add(b);				
				}	
			}			
		}
		return bList;
	}

	public ArrayList<Book> getAvailableBooks() {
		ArrayList<Book> toReturn = new ArrayList<Book>();
		for (Book book : this.getBooks()) {
			if(book.getPerson() == null)
				toReturn.add(book);			
		}
		return toReturn;
	}

	public ArrayList<Book> getUnAvailableBooks() {
		ArrayList<Book> toReturn = new ArrayList<Book>();
		for (Book book : this.getBooks()) {
			if(book.getPerson() != null)
				toReturn.add(book);
		}
		return toReturn;
	}
	
	public String toString(){
		return this.getName() + ": " +
			   this.getBooks().size() + " Books, "+
			   this.getPeople().size()+ " People.";
	}

	
	public static void main(String[] args) {
		MyLibrary 	m1 = new MyLibrary("Test Lib");
		Book 		b1 = new Book("Book1");
		Book 		b2 = new Book("Book2");
		Person 		p1 = new Person();
		Person 		p2 = new Person();
		p1.setName("Ibr");
		p2.setName("Qud");
		b1.setAuthor("Author 1");
		b1.setAuthor("Author 2");
		m1.addBook(b1);
		m1.addBook(b2);
		m1.addPerson(p1);
		m1.addPerson(p2);
		
		System.out.println("Just started using my new Library...");
		m1.printStatus();
		
		System.out.println("Checking out Book1...");
		m1.checkout(b1, p1);
		m1.printStatus();
		
		System.out.println("Checking in Book1 and Checking out Book2...");
		m1.checkin(b1);
		m1.checkout(b2, p2);
		m1.printStatus();		
		
	}

	private void printStatus() {

			System.out
					.println("Printing the Status of " + this.getName());
			
			for (Book mybook : this.getBooks()) {
				System.out.println(mybook);
			}
			
			for (Person myperson :this.getPeople() ) {
				System.out.println(myperson.getName() + " (Limit:"+myperson.getMaximumBooks()+") has " + 
									this.getNumberOfCheckedoutBooksForPerson(myperson) 
									+ " of my books.");
			}
			
			System.out.println("Total available books are "
					+ this.getAvailableBooks().size());
			
			 System.out.println("*******End of status report for " + this.getName()+ "*********\n\n\n");
	}


}
